part of '../repository.dart';

class _PurchasedItemInfoDtoFactory {
  const _PurchasedItemInfoDtoFactory._internal();

  static const _instance = _PurchasedItemInfoDtoFactory._internal();

  factory _PurchasedItemInfoDtoFactory() {
    return _instance;
  }

  _PurchasedItemInfoDto fromJson(Map<String, dynamic> json) {
    final type = json[_kPurchasedItemInfoTypeKey] as String;

    if (type == _kOneTimePurchasedItemInfoTypeValue) {
      return _OneTimePurchasedItemInfoDto.fromJson(json);
    }

    if (type == _kPurchasedSubscriptionInfoTypeValue) {
      return _PurchasedSubscriptionInfoDto.fromJson(json);
    }

    throw UnimplementedError();
  }

  _PurchasedItemInfoDto fromEntity(GooglePlayPurchasedItemInfo purchasedItemInfo) {
    if (purchasedItemInfo is GooglePlayOneTimePurchasedItemInfo) {
      return _OneTimePurchasedItemInfoDto.fromEntity(purchasedItemInfo);
    }

    if (purchasedItemInfo is GooglePlayPurchasedSubscriptionInfo) {
      return _PurchasedSubscriptionInfoDto.fromEntity(purchasedItemInfo);
    }

    throw UnimplementedError();
  }
}
